package com.neu.service;

import com.github.pagehelper.PageHelper;
import com.neu.common.GoodsAllinone;
import com.neu.dao.ShoppingGoodsMapper;
import com.neu.domain.*;
import com.neu.util.response.BaseResponse;
import com.neu.util.response.ListVoUntil;
import com.neu.vo.CatVo;
import com.neu.vo.ShoppingCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品管理 秦佳宇
 */
@Service
@Transactional
public class ShoppingGoodsService {
    @Resource
    private ShoppingGoodsMapper shoppingGoodsMapper;
    @Autowired
    private ShoppingCategoryService categoryService;
    @Autowired
    private LitemallBrandService brandService;

    public List<ShoppingGoods> list(Integer limit, Integer page, String sort, String order,Integer goodsSn,String name) {
        PageHelper.startPage(page,limit);
        return shoppingGoodsMapper.list(sort,order,goodsSn,name);
    }

    public ShoppingGoods detail(Integer id) {
        return shoppingGoodsMapper.selectByExampleWithBLOBs(id);
    }




    public int delete(ShoppingGoods shoppingGoods) {
        return shoppingGoodsMapper.delete(shoppingGoods);
    }


    public int update(ShoppingGoods shoppingGoods) {
        return shoppingGoodsMapper.update(shoppingGoods);
    }

    public List<ShoppingGoodsSpecification> specifications(ShoppingGoodsSpecification shoppingGoodsSpecification) {
        return shoppingGoodsMapper.specifications(shoppingGoodsSpecification);
    }

    public List<ShoppingGoodsProduct> products(ShoppingGoodsProduct shoppingGoodsProduct) {
        return shoppingGoodsMapper.products(shoppingGoodsProduct);
    }

    public List<ShoppingGoodsAttribute> attributes(ShoppingGoodsAttribute shoppingGoodsAttribute) {
        return shoppingGoodsMapper.attributes(shoppingGoodsAttribute);
    }

    public Object list2() {
        // http://element-cn.eleme.io/#/zh-CN/component/cascader
        // 管理员设置“所属分类”
        List<ShoppingCategory> l1CatList = categoryService.queryL1();
        List<CatVo> categoryList = new ArrayList<>(l1CatList.size());

        for (ShoppingCategory l1 : l1CatList) {
            CatVo l1CatVo = new CatVo();
            l1CatVo.setValue(l1.getId());
            l1CatVo.setLabel(l1.getName());

            List<ShoppingCategory> l2CatList = categoryService.queryByPid(l1.getId());
            List<CatVo> children = new ArrayList<>(l2CatList.size());
            for (ShoppingCategory l2 : l2CatList) {
                CatVo l2CatVo = new CatVo();
                l2CatVo.setValue(l2.getId());
                l2CatVo.setLabel(l2.getName());
                children.add(l2CatVo);
            }
            l1CatVo.setChildren(children);

            categoryList.add(l1CatVo);
        }

        // http://element-cn.eleme.io/#/zh-CN/component/select
        // 管理员设置“所属品牌商”
        List<LitemallBrand> list = brandService.all();
        List<Map<String, Object>> brandList = new ArrayList<>(l1CatList.size());
        for (LitemallBrand brand : list) {
            Map<String, Object> b = new HashMap<>(2);
            b.put("value", brand.getId());
            b.put("label", brand.getName());
            brandList.add(b);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("categoryList", categoryList);
        data.put("brandList", brandList);
        return BaseResponse.generateOKResponseEntity(data);
    }




    public int addSpecification(ShoppingGoodsSpecification specification) {
        return shoppingGoodsMapper.addSpecification(specification);
    }

    public int addAttribute(ShoppingGoodsAttribute attribute) {
        return shoppingGoodsMapper.addAttribute(attribute);
    }

    public int addProduct(ShoppingGoodsProduct product) {
        return shoppingGoodsMapper.addProduct(product);
    }

    public Object create(GoodsAllinone goodsAllinone) {
        ShoppingGoods goodsVoList = goodsAllinone.getGoods();
        ShoppingGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        ShoppingGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        ShoppingGoodsProduct[] products = goodsAllinone.getProducts();
        int count3 = shoppingGoodsMapper.insertGoods(goodsVoList);
        for (ShoppingGoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsVoList.getId());
            int count = shoppingGoodsMapper.addSpecification(specification);
        }

        for (ShoppingGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsVoList.getId());
            int count1 = shoppingGoodsMapper.addAttribute(attribute);
        }
        // 商品货品表litemall_product
        for (ShoppingGoodsProduct product : products) {
            product.setGoodsId(goodsVoList.getId());
            int count2 = shoppingGoodsMapper.addProduct(product);
        }
        return BaseResponse.generateOKResponseEntity();
    }

    public List<ShoppingGoods> queryByIds(Integer[] goodsIds) {
        return null;
    }
}